package com.hand.wfive.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.view.View;

import androidx.annotation.Nullable;

import com.hand.wfive.R;
import com.hand.wfive.view.dialog.UpProgressView;
import com.hjq.permissions.XXPermissions;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.qmuiteam.qmui.arch.QMUIActivity;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

import butterknife.ButterKnife;

public abstract class BaseActivity extends QMUIActivity {

    // tip持续时长
    public static final int TIP_DURATION = 1000;

    protected abstract int getContentViewId();

    protected BaseActivity mActivity;
    protected Context mContext;

    private QMUITipDialog loadingDialog;
    private QMUITipDialog tipDialog;
    protected final Handler mBaseHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isStatusTextBlack()) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        } else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this);
        }
        mContext = this;
        mActivity = this;
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        setContentViewBefore();
        setContentView(getContentViewId());
        ButterKnife.bind(this);
        init();
    }

    protected abstract void init();

    protected boolean isStatusTextBlack() {
        return true;
    }

    protected void setContentViewBefore() {
    }

    protected void turnSystemPermissionBack() {
        System.out.println("BaseActivity-turnSystemPermissionBack");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE) {
            turnSystemPermissionBack();
        } else {
            doOnActivityResult(requestCode, resultCode, data);
        }
    }

    protected void doOnActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

    }

    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
    protected <T extends Parcelable> T getParcelableExtra(String name) {
          return getParcelableExtra(getIntent(), name);
      }
    protected <T extends Parcelable> T getParcelableExtra(Intent intent, String name) {
          if (intent == null) return null;
          return intent.getParcelableExtra(name);
      }

      protected Serializable getSerializableExtra(String name) {
          return getSerializableExtra(getIntent(), name);
      }

      protected Serializable getSerializableExtra(Intent intent, String name) {
          if (intent == null) return null;
          return intent.getSerializableExtra(name);
      }
    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
        if (tipDialog != null) {
            tipDialog.cancel();
            tipDialog = null;
        }
        if (loadingDialog != null) {
            loadingDialog.cancel();
            loadingDialog = null;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
    }

    protected void showLoading() {
        showLoading("");
    }

    protected void showLoading(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create();
        loadingDialog.show();
    }

    protected void showLoadingC() {
        showLoadingC("");
    }

    protected void showLoadingC(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create(false);
        loadingDialog.show();
    }

    protected void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    protected void showLoadingTipC() {
        showLoadingTipC("");
    }

    protected void showLoadingTipC(String msg) {
        showLoadingTip(false, msg);
    }

    private void showLoadingTip(boolean cancelable, String msg) {
        dismissTip();
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create(cancelable);
        tipDialog.show();
    }

    protected void showNormalTip(String msg) {

        dismissTip();
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        mBaseHandler.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showErrorTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showErrorTip(String msg) {
        dismissTip();
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        mBaseHandler.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showSuccessTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showNormalTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    BasePopupView basePopupView;

    protected void showLoadingProgress() {
        if (basePopupView != null && basePopupView.isShow()) {
            basePopupView.dismiss();
            basePopupView = null;
        }
        basePopupView = new XPopup.Builder(this)
                .dismissOnBackPressed(false)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .asCustom(new UpProgressView(this));
        basePopupView.show();
    }
    protected void dismissLoadingProgress() {
          if (basePopupView != null && basePopupView.isShow()) {
              basePopupView.dismiss();
              basePopupView = null;
          }
      }
    protected void showNoIconTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_NOTHING)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void dismissTip() {
        dismissLoadingProgress();
        if (tipDialog != null && tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }

}
